package main

import (
	"encoding/base64"
	"html/template"

	//"encoding/json"
	"fmt"
	"net/http"
	"time"
)


func setMessageHandler(w http.ResponseWriter, r *http.Request) {
	msg := []byte("Hello World")
	c := http.Cookie{
		Name: "flash",
		Value:base64.URLEncoding.EncodeToString(msg),
	}
	http.SetCookie(w, &c)
}

func getMessageHandler(w http.ResponseWriter, r *http.Request) {
	c, err := r.Cookie("flash")
	if err != nil {
		if err == http.ErrNoCookie {
			fmt.Fprintln(w, "No message found")
		}
	}else{
		rc := http.Cookie{
			Name: "flash",
			MaxAge: -1,
			Expires:time.Unix(1, 0),
		}
		http.SetCookie(w, &rc)
		val, _ := base64.URLEncoding.DecodeString(c.Value)
		fmt.Fprintln(w, string(val))
	}
}
func welcome(w http.ResponseWriter, r *http.Request) {
	t, err := template.ParseFiles("hh.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	t.Execute(w, nil)

}
func main() {
	http.HandleFunc("/",welcome)
	http.HandleFunc("/g", setMessageHandler)
	http.HandleFunc("/r", getMessageHandler)

	http.ListenAndServe(":9090", nil)
}
